package com.jt.tenant.picker;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.bigkoo.pickerview.R;
import com.bigkoo.pickerview.lib.NumericWheelAdapter;
import com.bigkoo.pickerview.lib.OnWheelChangedListener;
import com.bigkoo.pickerview.lib.WheelView;

import android.content.Context;
import android.util.Log;
import android.view.View;


public class WheelTime {
    public SimpleDateFormat startSimple = new SimpleDateFormat("yyyy-MM-dd");
    public SimpleDateFormat endSimple = new SimpleDateFormat("yyyy-MM-dd");
    public List<String> big = Arrays.asList(new String[]{ "1", "3", "5", "7", "8", "10", "12" });
    public List<String> little = Arrays.asList(new String[]{ "4", "6", "9", "11" });
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private View view;
    private WheelView wv_year;
    private WheelView wv_month;
    private WheelView wv_day;
    private WheelView wv_hours;
    private WheelView wv_mins;
    public int screenheight;

    private TimePopupWindow.Type type;

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public WheelTime(View view) {
        super();
        this.view = view;
        type = TimePopupWindow.Type.ALL;
        setView(view);
    }
    public WheelTime(View view,TimePopupWindow.Type type, Date start, Date end) {
        super();
        this.view = view;
        this.type = type;
        setStarEndTime(start, end);
        setView(view);
    }

    public int sY = 1970, sM = 1, sD = 1;
    public int eY = 2100, eM = 12, eD = 31;
    public void setStarEndTime(Date start, Date end){
        String[] array = null;
        if(start != null){
            array = startSimple.format(start).split("\\-");
            sY = toInteger(array[0]);
            sM = toInteger(array[1]);
            sD = toInteger(array[2]);
        }
        if(end != null){
            array = endSimple.format(end).split("\\-");
            eY = toInteger(array[0]);
            eM = toInteger(array[1]);
            eD = toInteger(array[2]);
        }
    }
    public int toInteger(String i){
        return Integer.valueOf(i);
    }

    public void setPicker(int year ,int month,int day){
        this.setPicker(year, month, day, 0, 0);
    }

    private int tempM = 0, tempD = 0;
    /**
     * @Description: TODO 弹出日期时间选择器
     */
    public void setPicker(int year ,int month ,int day,int h,int m) {
        // 添加大小月月份并将其转换为list,方便之后的判断
        String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
        String[] months_little = { "4", "6", "9", "11" };

        final List<String> list_big = Arrays.asList(months_big);
        final List<String> list_little = Arrays.asList(months_little);

        Context context = view.getContext();
        // 年
        wv_year = (WheelView) view.findViewById(R.id.year);
        wv_year.setAdapter(new NumericWheelAdapter(sY, eY));// 设置"年"的显示数据
        wv_year.setLabel(context.getString(R.string.pickerview_year));// 添加文字
        wv_year.setCurrentItem(year - sY);// 初始化时显示的数据

        int[] datee = getShowTime(year, month + 1);

        // 月
        wv_month = (WheelView) view.findViewById(R.id.month);
        wv_month.setLabel(context.getString(R.string.pickerview_month));
        wv_month.setAdapter(new NumericWheelAdapter(tempM = datee[0], datee[1]));
        wv_month.setCurrentItem(month + 1 == datee[1] ? datee[1] - datee[0] : 0);

        // 日
        wv_day = (WheelView) view.findViewById(R.id.day);
        // 判断大小月及是否闰年,用来确定"日"的数据
        wv_day.setAdapter(new NumericWheelAdapter(tempD = datee[2], datee[3]));
        wv_day.setLabel(context.getString(R.string.pickerview_day));
        wv_day.setCurrentItem(day == datee[3] ? datee[3] - datee[2] : 0);

        wv_hours = (WheelView)view.findViewById(R.id.hour);
        wv_hours.setAdapter(new NumericWheelAdapter(0, 23));
        wv_hours.setLabel(context.getString(R.string.pickerview_hours));// 添加文字
        wv_hours.setCurrentItem(h);

        wv_mins = (WheelView)view.findViewById(R.id.min);
        wv_mins.setAdapter(new NumericWheelAdapter(0, 59));
        wv_mins.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字
        wv_mins.setCurrentItem(m);

        // 添加"年"监听
        OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
            int[] datee;
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                datee = getShowTime(newValue + sY, 0);
                wv_month.setAdapter(new NumericWheelAdapter(tempM = datee[0], datee[1]));
                datee = getShowTime(newValue + sY, wv_month.getCurrentItem() + tempM);
                wv_day.setAdapter(new NumericWheelAdapter(tempD = datee[2], datee[3]));
                if (wv_day.getCurrentItem() > datee[3] - datee[2]){
                    wv_day.setCurrentItem(datee[3] - datee[2]);
                }
            }
        };

        // 添加"月"监听
        OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
            int[] datee;
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                datee = getShowTime(wv_year.getCurrentItem() + sY, newValue + tempM);
                wv_day.setAdapter(new NumericWheelAdapter(tempD = datee[2], datee[3]));
                if (wv_day.getCurrentItem() > datee[3] - datee[2]){
                    wv_day.setCurrentItem(datee[3] - datee[2]);
                }
            }
        };

        wv_year.addChangingListener(wheelListener_year);
        wv_month.addChangingListener(wheelListener_month);

        // 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
        int textSize = 0;
        switch(type){
            case ALL:
                textSize = (screenheight / 100) * 3;
                break;
            case YEAR_MONTH_DAY:
                textSize = (screenheight / 100) * 3;
                wv_hours.setVisibility(View.GONE);
                wv_mins.setVisibility(View.GONE);
                break;
            case HOURS_MINS:
                textSize = (screenheight / 100) * 4;
                wv_year.setVisibility(View.GONE);
                wv_month.setVisibility(View.GONE);
                wv_day.setVisibility(View.GONE);
                break;
            case MONTH_DAY_HOUR_MIN:
                textSize = (screenheight / 100) * 3;
                wv_year.setVisibility(View.GONE);
                break;
            case YEAR:
                textSize = (screenheight / 100) * 4;
                wv_month.setVisibility(View.GONE);
                wv_day.setVisibility(View.GONE);
                wv_hours.setVisibility(View.GONE);
                wv_mins.setVisibility(View.GONE);
                break;
            case YEAR_MONTH:
                textSize = (screenheight / 100) * 4;
                wv_day.setVisibility(View.GONE);
                wv_hours.setVisibility(View.GONE);
                wv_mins.setVisibility(View.GONE);
                break;
        }

        wv_day.TEXT_SIZE = textSize;
        wv_month.TEXT_SIZE = textSize;
        wv_year.TEXT_SIZE = textSize;
        wv_hours.TEXT_SIZE = textSize;
        wv_mins.TEXT_SIZE = textSize;

    }

    private int[] getShowTime(int year ,int month){

        Log.e("getShowTime", year + " " + month);

        int yearType = 0, monthType = 0;

        int sMM = 1, eMM = 12, sDD = 1, eDD = 31;

        if(year == sY && year == eY){

            sMM = sM;

            eMM = eM;

            yearType = 1;

        }else if(year == sY){

            sMM = sM;

            yearType = 2;

        }else if(year == eY){

            eMM = eM;

            yearType = 3;

        }

        Log.e("yearType", yearType + "");

        switch (yearType){

            case 1:

                if(month == sM && month == eM){

                    monthType = 1;

                }else if(month == sM){

                    monthType = 2;

                }else if(month == eM){

                    monthType = 3;

                }

                break;

            case 2:

                if(month == sM){

                    monthType = 2;

                }

                break;

            case 3:

                if(month == eM){

                    monthType = 3;

                }

                break;

        }

        Log.e("monthType", monthType + "");

        switch (monthType){

            case 0:

                if (big.contains(String.valueOf(month))) {

                    sDD = 1;

                    eDD = 31;

                } else if (little.contains(String.valueOf(month))) {

                    sDD = 1;

                    eDD = 30;

                } else {

                    if ((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0){

                        sDD = 1;

                        eDD = 29;

                    }else{

                        sDD = 1;

                        eDD = 28;

                    }

                }

                break;

            case 1:

                sDD = sD;

                eDD = eD;

                break;

            case 2:

                sDD = sD;

                if (big.contains(String.valueOf(month))) {

                    eDD = 31;

                } else if (little.contains(String.valueOf(month))) {

                    eDD = 30;

                } else {

                    if ((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0){

                        eDD = 29;

                    }else{

                        eDD = 28;

                    }

                }

                break;

            case 3:

                sDD = 1;

                eDD = eD;

                break;

        }

        Log.e("getShowTime", sMM + " " + eMM + " " + sDD + " " + eDD);

        return new int[]{sMM, eMM, sDD, eDD};

    }

    /**
     * 设置是否循环滚动
     * @param cyclic
     */
    public void setCyclic(boolean cyclic){
        wv_year.setCyclic(cyclic);
        wv_month.setCyclic(cyclic);
        wv_day.setCyclic(cyclic);
        wv_hours.setCyclic(cyclic);
        wv_mins.setCyclic(cyclic);
    }
    public String getTime() {
        StringBuffer sb = new StringBuffer();
        sb.append((wv_year.getCurrentItem() + sY)).append("-")
                .append((wv_month.getCurrentItem() + tempM)).append("-")
                .append((wv_day.getCurrentItem() + tempD)).append(" ")
                .append(wv_hours.getCurrentItem()).append(":")
                .append(wv_mins.getCurrentItem());
        return sb.toString();
    }
}
